<?php

class User extends Controller {

	//function __construct()
        function User()
        {
                parent::Controller();
                $this->load->model('test_model');

                $this->load->library('form_validation');
        }

        //Login function that checks the usernames and passwords with the database records
        function Login()
        {
            $data = array();

            if (isset($_POST['login']))
            {
                //Validate * Prep
                $this->load->library('form_validation');
                $this->form_validation->set_rules('login', 'Login Name', 'required|trim|strtolower');
                $this->form_validation->set_rules('pass', 'Password', 'required|trim');

                $result = $this->form_validation->run();

                if ($result === TRUE)
                {
                    //Check the username and password against the database
                    $this->load->model('test_model');
                    $result = $this->test_model->check_credentials($_POST['login'], $_POST['pass']);

                    //If they match, then add a variable to the session cookie
                    if ($result !== FALSE)
                    {
                        $this->session->set_userdata(array('is_logged_in' => $result));

                        

                        if (check_user_role() == '1')
                            redirect('user/adminmain');
                        else
                            redirect('user/usermain');
                    }
                        else
                            $data['error_msg'] = "The username or password was incorrect!";
                        }
                        else
                            $data['error_msg'] = "There was a problem with your submission!";
                }

                $this->load->view('user/login', $data);
        }

        function adminmain()
        {
            $this->load->view('admin/adminmain');
        }

        function usermain()
        {
            $this->load->view('user/usermain');
        }
        
        //Image function for uploading images.  Validating and preping those images.
        function Image()
        {
                 
            $this->load->helper('form');
            //not sure what these lines are doing.
                if (isset($_POST['image']))
                {
                        $image = $this->input->post('image');
                }

                //Validate and prep
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '500';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                       // $error = array('error' => $this->upload->display_errors());

                       // echo "<h1>ERROR</h1><p>" .var_dump($error) ."</p>";
                       
                }
                else
                {
                        //Success!
                        $data = array('upload_data' => $this->upload->data());

                        $upload_data = $this->upload->data();
                        $file_path = $upload_data['file_name'];

                        //INSERT CODE HERE TO STORE IN THE DATABASE.
                        //echo "<p>Successfully added story.</p>";
                        //$this->load->model('test_model');

                        //$file_path = $this->test_model->add_image($_POST['image']);

                        echo "<h1>Success!</h1>";
                        //echo "<p>The File Name is $file_path";
                }

                $this->load->view('user/upload');
        }

        //Story function prepping and validating the input of stories by users.

        function Story()
        {
            $this->load->library('form_validation');
            
            if ($this->input->post('submit_story'))
            {
                //Validate & Prep
                $this->form_validation->set_rules('story', 'story field', 'required|max_length[200]');

                $results = $this->form_validation->run();

                if ($results == TRUE)
                {

                    //echo "<p>Successfully added story.</p>";
                    $this->load->model('test_model');

                    $result = $this->test_model->add_story($_POST['story']);

                        if ($result === TRUE)
                            redirect('user/usermain');
                        else
                            echo "";

                }
                    else
                        {
                            //echo "<p>It failed.</p>";
                            //echo "<p>" . validation_errors() . "</p>";
                        }
                }

                $this->load->view('user/story');
        }

        function liststory()
        {
            //Load up my model and get the user records
            $this->load->model('test_model');
            $user_records = $this->test_model->list_story();

            //Prepare the data array to send data to the view
            $data = array();
            $data['results'] = $user_records;

            //Load the view, sending it my data
            $this->load->view('user/usermain', $data);
        }

}

/* EOF */